package com.g2room.tools.mybatisTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.g2room.tools.db.db2po.DataSourceModel;
import com.g2room.tools.db.db2po.DbUtil;
import com.g2room.tools.db.db2po.PropertiesUtil;
import com.g2room.tools.db.db2po.StringUtil;
import com.g2room.tools.mybatisTools.to.Field;
import com.g2room.tools.mybatisTools.to.XmlMapper;

public class TableInfo {
	// 包名
	private static String packageStr = "com.carry6.adps.model.po.";
	private static String basePkg = "com.carry6.adps.";
	
	private static Map<String, String> tableNameMap = new HashMap<String, String>();
	static {
		tableNameMap.put("lol_competition","competition");
		tableNameMap.put("lol_comp_event","competition_event");
		tableNameMap.put("lol_comp_event_player","competition_event_player");
		tableNameMap.put("lol_comp_event_stage","competition_event_stage");
		tableNameMap.put("lol_comp_event_team","competition_event_team");
		tableNameMap.put("lol_game_ban","game_ban");
		tableNameMap.put("lol_one_comp_event_coll","lol_one_comp_event");
		tableNameMap.put("lol_one_comp_event","one_competition_event");
		tableNameMap.put("lol_one_comp_honour","one_competition_honour");
		tableNameMap.put("lol_one_comp","one_competition");
		tableNameMap.put("lol_one_comp_player","one_competition_lol_player");	
		tableNameMap.put("lol_one_comp_team","one_competition_lol_team");
		tableNameMap.put("lol_player","player");
		tableNameMap.put("lol_summ_skill_hst","lol_summoner_skill_hst");
		tableNameMap.put("lol_summ_skill","lol_summoner_skill");
		tableNameMap.put("lol_team","team");
		 
		tableNameMap.put("dota2_competition","competition_dota2");
		tableNameMap.put("dota2_comp_event","competition_event_dota2");
		tableNameMap.put("dota2_comp_event_player","competition_event_player_dota2");
		tableNameMap.put("dota2_comp_event_stage","competition_event_stage_dota2");
		tableNameMap.put("dota2_comp_event_team","competition_event_team_dota2");	
		tableNameMap.put("dota2_game_ban","game_ban_dota2");
		tableNameMap.put("dota2_one_comp","one_competition_dota2");
		tableNameMap.put("dota2_one_comp_event_coll","dota2_one_comp_event");
		tableNameMap.put("dota2_one_comp_event","one_competition_event_dota2");
		tableNameMap.put("dota2_one_comp_honour","one_competition_honour_dota2");
		tableNameMap.put("dota2_one_comp_player","one_competition_dota2_player");
		tableNameMap.put("dota2_one_comp_player_to_player","one_competition_dota2_player_to_player");
		tableNameMap.put("dota2_one_comp_team","one_competition_dota2_team");
		tableNameMap.put("dota2_player","player_dota2");
		tableNameMap.put("dota2_team","team_dota2");
		
		tableNameMap.put("dota2_team_info","dota2_team_info");
		tableNameMap.put("lol_team_info","lol_team_info");
		tableNameMap.put("lol_hero","lol_hero");
		tableNameMap.put("game_position","game_position");
		tableNameMap.put("lol_hero_skill","lol_hero_skill");
		tableNameMap.put("dota2_hero_restraint","dota2_hero_restraint");
		tableNameMap.put("lol_rune_tag","lol_rune_tag");
		tableNameMap.put("dota2_stat_team","dota2_stat_team");
		tableNameMap.put("lol_hero_hst","lol_hero_hst");
		tableNameMap.put("game_event","game_event");
		tableNameMap.put("dota2_hero_skill","dota2_hero_skill");
		tableNameMap.put("lol_stat_team","lol_stat_team");
		tableNameMap.put("honour_call","honour_call");
		tableNameMap.put("lol_rune_hst","lol_rune_hst");
		tableNameMap.put("lol_equipment","lol_equipment");
		tableNameMap.put("lol_equipment_hst","lol_equipment_hst");
		tableNameMap.put("lol_hero_restraint","lol_hero_restraint");
		tableNameMap.put("area","area");
		tableNameMap.put("dota2_series","dota2_series");
		tableNameMap.put("lol_equipment_tag","lol_equipment_tag");
		tableNameMap.put("lol_series","lol_series");
		tableNameMap.put("lol_hero_tag","lol_hero_tag");
		tableNameMap.put("lol_version","lol_version");
		tableNameMap.put("lol_mastery_hst","lol_mastery_hst");
		tableNameMap.put("animal_type","animal_type");
		tableNameMap.put("dota2_hero","dota2_hero");
		tableNameMap.put("lol_hero_skill_hst","lol_hero_skill_hst");
		tableNameMap.put("nationality","nationality");
		tableNameMap.put("dota2_equipment","dota2_equipment");
	}
	
	public static void main(String[] args) {
		getTableInfo();
	}
	public static DbUtil getDbUtil() {
		DataSourceModel ds = new DataSourceModel();
		ds.setDriver(PropertiesUtil.getDBConfByKey("DB.Driver"));
		ds.setUsername(PropertiesUtil.getDBConfByKey("DB.UserName"));
		ds.setPassword(PropertiesUtil.getDBConfByKey("DB.PassWord"));
		ds.setUrl(PropertiesUtil.getDBConfByKey("DB.URL"));
		return new DbUtil(ds);
	}
	/**
	 * 生成java文件
	 *
	 * @auther <a href="mailto:g2room@163.com">天涯</a>
	 * Jan 9, 2014 5:08:05 PM
	 */
	public static List<XmlMapper> getTableInfo() {
		DbUtil dbUtil = getDbUtil();
		Map<String, String> tables = dbUtil.getTablesByStatus();
		
		boolean isComment = true;
		List<XmlMapper> xmlMapperList = new ArrayList<XmlMapper>();
		for (String tableName : tables.keySet()) {
			XmlMapper xmlMapper = new XmlMapper();
			List<Field> fields = new ArrayList<Field>();
			String tn = StringUtil.hump4Underline(tableName);
			String suff = "";
			if(tn.startsWith("lol")) {
				suff = "lol";
			} else if(tn.startsWith("dota2")) {
				suff = "dota2";
			}
			// 此处写死ID
			xmlMapper.setIdField("${id}");
			String minusculeTn = StringUtil.minuscule(tn.replaceAll("lol", "").replaceAll("dota2", "")) + (StringUtil.isBlank(suff) ? "" : StringUtil.capitalize(suff));
			String capitalizeTn = StringUtil.capitalize(tn.replaceAll("lol", "").replaceAll("dota2", "")) +  (StringUtil.isBlank(suff) ? "" : StringUtil.capitalize(suff));
			xmlMapper.setAlias(minusculeTn);
			xmlMapper.setPoName(packageStr + (StringUtil.isBlank(suff) ? "" : suff + ".") + capitalizeTn);
			xmlMapper.setPkgMapper(basePkg + "mapper" +"." + (StringUtil.isBlank(suff) ? "" : suff + ".") + capitalizeTn + "Mapper");
			String dbTableName = tableNameMap.get(tableName);
			if(null != dbTableName) {
				xmlMapper.setTableName(dbTableName);
			} else {
//				System.out.println("tableNameMap.put(\""+tableName+"\",\""+tableName+"\");");
				System.out.println(tableName + "表未找到" + dbTableName);
			}
			xmlMapper.setMapName(minusculeTn + "Map");
			xmlMapper.setPkgServiceName(basePkg + "service" + (StringUtil.isBlank(suff) ? "" :  "."+suff));
			xmlMapper.setPkgMapperName(basePkg + "mapper" + (StringUtil.isBlank(suff) ? "" :  "."+suff));
//			xmlMapper.setXmlName(StringUtil.hump4Underline(tableName));
			xmlMapper.setXmlName((StringUtil.isBlank(suff) ? "" : suff + "/") + capitalizeTn);
			tn = StringUtil.capitalize(tn);
			Map<String, String> colns = dbUtil.getColumns(tableName, isComment);
			String column = "";
			for (String key : colns.keySet()) {
				if(isComment) {
					column = key.substring(0, key.indexOf(" "));
				} else {
					column = key;
				}
				String fn = StringUtil.hump4Underline(column);
				Field fieldTo = new  Field();
				fieldTo.setColumn(column);
				fieldTo.setFieldName(fn);
				fields.add(fieldTo);
			}
			xmlMapper.setFields(fields);
			xmlMapperList.add(xmlMapper);
		}
		return xmlMapperList;
	}
}
